<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>车类型编辑</title>
    <link href="//cdn.staticfile.org/layui/2.9.9/css/layui.css" rel="stylesheet">
    <script src="jq.js"></script>
    <script>
        function getQueryString(name) {
            const URL = new URLSearchParams(window.location.search)
            let val = URL.get(name)
            return val
        }

        let tid = getQueryString('tid')

        $(function (){
            if(tid != null) {
                $.getJSON("http://localhost:8080/cartype/one/" + tid, function (res) {
                    console.log(res)
                    $("#tid").val(tid)
                    $("#name").val(res.data.name)
                })
            }else{
                $("#tidbox").hide()
            }
        })

    </script>
</head>
<body>
<script src="//cdn.staticfile.org/layui/2.9.9/layui.js"></script>
<div class="layui-fluid">
    <div class="layui-row">
        <div class="layui-col-md10 layui-col-md-offset1">
            <h1>layui</h1>
            <form class="layui-form layui-form-pane" action="">
                <div id="tidbox" class="layui-form-item">
                    <label class="layui-form-label">类型id</label>
                    <div class="layui-input-inline">
                        <input type="text" id="tid" name="tid" autocomplete="off"   class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">类型名称</label>
                    <div class="layui-input-inline">
                        <input type="text" id="name" name="name" autocomplete="off" placeholder="请输入" lay-verify="required" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn" lay-submit lay-filter="demo1">确认</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'laydate', 'util'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;

        // 提交事件
        form.on('submit(demo1)', function(data){
            var field = data.field; // 获取表单字段值
            // 显示填写结果，仅作演示用
            // layer.alert(JSON.stringify(field), {
            //     title: '当前填写的字段值'
            // });
            // 此处可执行 Ajax 等操作
            // …
            if(tid != null){
                //修改
                $.post("http://localhost:8080/cartype/update",{
                    tid:tid,
                    name:field.name
                },function (res){
                    console.log(res)
                    // alert('修改成功')
                    layer.msg('修改成功', {icon: 1});
                })
            }else{
                //新增
                $.post("http://localhost:8080/cartype/add",{
                    name:field.name
                },function (res){
                    console.log(res)
                    // alert('新增成功,tid:'+res.tid)
                    layer.msg('新增成功,tid:'+res.tid, {icon: 1});
                })
            }
            return false; // 阻止默认 form 跳转
        });




    });
</script>
</body>
</html>